package com.anyou.mall.use.service.impl;

import java.util.List;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.index.param.MsgParam;
import com.anyou.mall.index.param.UserMsgParam;
import com.anyou.mall.util.AssertUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.use.mapper.UserSystemNoticeMapper;
import com.anyou.mall.use.domain.UserSystemNotice;
import com.anyou.mall.use.service.IUserSystemNoticeService;

/**
 * 用户消息Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-04-22
 */
@Service
public class UserSystemNoticeServiceImpl implements IUserSystemNoticeService
{
    @Autowired
    private UserSystemNoticeMapper userSystemNoticeMapper;

    /**
     * 查询用户消息
     *
     * @param systemNoticeId 用户消息主键
     * @return 用户消息
     */
    @Override
    public UserSystemNotice selectUserSystemNoticeBySystemNoticeId(Long systemNoticeId)
    {
        return userSystemNoticeMapper.selectUserSystemNoticeBySystemNoticeId(systemNoticeId);
    }

    /**
     * 查询用户消息列表
     *
     * @param userSystemNotice 用户消息
     * @return 用户消息
     */
    @Override
    public List<UserSystemNotice> selectUserSystemNoticeList(UserSystemNotice userSystemNotice)
    {
        return userSystemNoticeMapper.selectUserSystemNoticeList(userSystemNotice);
    }

    /**
     * 新增用户消息
     *
     * @param userSystemNotice 用户消息
     * @return 结果
     */
    @Override
    public int insertUserSystemNotice(UserSystemNotice userSystemNotice)
    {
        userSystemNotice.setCreateTime(DateUtils.getNowDate());
        return userSystemNoticeMapper.insertUserSystemNotice(userSystemNotice);
    }

    /**
     * 修改用户消息
     *
     * @param userSystemNotice 用户消息
     * @return 结果
     */
    @Override
    public int updateUserSystemNotice(UserSystemNotice userSystemNotice)
    {
        return userSystemNoticeMapper.updateUserSystemNotice(userSystemNotice);
    }

    /**
     * 批量删除用户消息
     *
     * @param systemNoticeIds 需要删除的用户消息主键
     * @return 结果
     */
    @Override
    public int deleteUserSystemNoticeBySystemNoticeIds(Long[] systemNoticeIds)
    {
        return userSystemNoticeMapper.deleteUserSystemNoticeBySystemNoticeIds(systemNoticeIds);
    }

    /**
     * 删除用户消息信息
     *
     * @param systemNoticeId 用户消息主键
     * @return 结果
     */
    @Override
    public int deleteUserSystemNoticeBySystemNoticeId(Long systemNoticeId)
    {
        return userSystemNoticeMapper.deleteUserSystemNoticeBySystemNoticeId(systemNoticeId);
    }
    /**
     * 消息列表
     * @param param
     * @return
     */
    @Override
    public List<UserSystemNotice> getMsgList(UserMsgParam param) {
        return userSystemNoticeMapper.selectMsgList(param);
    }
    /**
     * 消息已读
     * @param param
     */
    @Override
    public void userReadMsg(MsgParam param) {
        UserSystemNotice userSystemNotice = userSystemNoticeMapper.selectById(param.getSystemNoticeId());
        AssertUtils.isNull(userSystemNotice,"消息不存在");
        userSystemNotice.setIsRead(1);
        userSystemNoticeMapper.updateById(userSystemNotice);
    }

    @Override
    public Integer countMsgList(UserMsgParam userMsgParam) {
        Integer num = userSystemNoticeMapper.selectCount(new LambdaQueryWrapper<UserSystemNotice>()
                .eq(UserSystemNotice::getSysUserId, userMsgParam.getSysUserId())
                .eq(userMsgParam.getIsRead() != null, UserSystemNotice::getIsRead, userMsgParam.getIsRead())
                .eq(userMsgParam.getNoticeType() != null, UserSystemNotice::getNoticeType, userMsgParam.getNoticeType()));
        return num;
    }
}
